package com.glasses.manage.auth.controller;

import com.glasses.manage.auth.entity.Music;
import com.glasses.manage.auth.service.MusicService;
import com.glasses.manage.utils.JSONUtil;
import com.glasses.manage.utils.MessageBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/9/26.
 */
@RequestMapping(value = {"/music"})
@Controller
public class MusicController {

    @Autowired
    private MusicService musicService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String manage_index() {
        return "music_manage";
    }

    /**
     * 获取默认启用的音乐
     *
     * @return
     */
    @RequestMapping(value = "/default", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getMusic() {
        List<Music> musics = musicService.getMusic();
        return JSONUtil.object2Json(musics);
    }

    /**
     * 查询音乐列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getMusicList() {
        Map<String, Object> map = new HashMap<>();
        List<Music> musics = musicService.listBy(map);
        return JSONUtil.object2Json(musics);
    }

    /**
     * 保存音乐
     *
     * @param musicId
     * @param musicLink
     * @param musicName
     * @param recStatus
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String saveMusic(Long musicId, String musicLink, String musicName, String recStatus) {
        if (musicLink != null && musicName != null) {
            if (musicId != null) {
                Music music = new Music();
                music.setId(musicId);
                music.setMusicLink(musicLink);
                music.setMusicName(musicName);
                music.setRecStatus(recStatus);
                musicService.update(music);
                return JSONUtil.object2Json(MessageBox.getSuccessMsg("保存成功！"));
            } else {
                Music music = new Music();
                music.setMusicLink(musicLink);
                music.setMusicName(musicName);
                musicService.create(music);
                return JSONUtil.object2Json(MessageBox.getSuccessMsg("新增成功！"));
            }

        }
        return JSONUtil.object2Json(MessageBox.getSuccessMsg("背景音乐链接和名称不能为空！"));
    }

    /**
     * 根据id停用音乐
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/stopMusic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String stop(Long id) {
        if (id == null) {
            return JSONUtil.object2Json(MessageBox.getErrorMsg("未选择音乐"));
        }
        Map map = new HashMap<>();
        map.put("id", id);
        musicService.delete(map, "stopMusic");
        return JSONUtil.object2Json(MessageBox.getSuccessMsg("停用成功"));
    }

    /**
     * 根据id启用音乐
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/startMusic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String start(Long id) {
        if (id == null) {
            return JSONUtil.object2Json(MessageBox.getErrorMsg("未选择音乐"));
        }
        Map map = new HashMap<>();
        map.put("id", id);
        musicService.delete(map, "stopAllMusic");
        musicService.delete(map, "startMusic");
        return JSONUtil.object2Json(MessageBox.getSuccessMsg("启用成功"));
    }

}
